package cn.tanglong.org.controller;

import cn.tanglong.basic.util.AjaxResult;
import cn.tanglong.basic.util.PageList;
import cn.tanglong.org.domain.Sdd;
import cn.tanglong.org.query.SddQuery;
import cn.tanglong.org.service.ISddService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sdd")
@Api(tags = "数据字典明细表", description = "明细品牌")
public class SddController {

    @Autowired
    private ISddService sddService;

    @GetMapping("{id}")
    @ApiOperation(value = "查询明细",notes = "根据id查询")
    public Sdd findOne(@PathVariable("id") Long id){
        return sddService.loadById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有明细",notes = "查询所有信息")
    public List<Sdd> findAll(){
        return sddService.loadAll();
    }

    // 分页查询
    // RequestBody 把请求体中的数据调整成json格式，封装到query中
    @PostMapping
    @ApiOperation(value = "查询分页",notes = "需要当前页和每页数据")
    public PageList<Sdd> queryPage(@RequestBody SddQuery query){
        return sddService.queryList(query);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除明细",notes = "根据id删除")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            sddService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程  装杯常用
            return AjaxResult.me().setMsg("操作失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "根据id数组删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            sddService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程  装杯常用
            return AjaxResult.me().setMsg("操作失败");
        }
    }

    @PutMapping()
    @ApiOperation(value = "明细添加或修改",notes = "如果有id就是修改，没id就是添加")
    public AjaxResult addOrUpdateById(@RequestBody Sdd sdd){
        try {
            if (sdd.getId() == null) {
                sddService.add(sdd);
            } else {
                sddService.update(sdd);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程  装杯常用
            return AjaxResult.me().setMsg("操作失败");
        }
    }
}
